package com.ljx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.Domain.Employee;
import com.ljx.Domain.R;
import com.ljx.Service.EmployeeService;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 登录模块
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> EmployeeLogin(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("登录模块");
        // 1.将密码使用md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户名查询数据
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee user = employeeService.getOne(lambdaQueryWrapper);
        //3.通过返回的数据判断用户是否存在，不存在则返回用户不存在
        if (user == null) {
            return R.error("用户不存在或密码错误，请重新输入");
        }
        //4.判断密码是否正确，不正确同理
        if (!password.equals(user.getPassword() )) {
            return R.error("用户不存在或密码错误，请重新输入");
        }
        //5.判断用户是否激活
        if (user.getStatus() != 1) {
            return R.error("用户没激活，请先激活");
        }
//        6.成功登录，将用户id存入session中,并返回登陆结果
        request.getSession().setAttribute("employee",user.getId());
        return  R.success(user);

    }

    /**
     * 退出模块
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> loginOut(HttpServletRequest request){
        log.info("退出模块");
        //删除session中数据
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 保存模块
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("保存模块");
        //设置员工其他默认属性
        //设置员工密码，默认为123456，使用md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //创建时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        //谁创建的用户
//        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        //调用service方法保存到数据库
        employeeService.save(employee);
        //返回成功对象
        log.info("添加员工成功");
        return R.success("添加成功");
    }


    /**
     * 分页模块
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> employeePage(Integer page,Integer pageSize,String name){
        log.info("分页模块");
        //设置分页数据
        Page<Employee> pages = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
        //设置分页条件
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        wrapper.orderByDesc(Employee::getCreateTime);
        //调用service层
        Page<Employee> employeePage = employeeService.page(pages, wrapper);
        return R.success(employeePage);
    }

    /**
     * 根据id更新数据
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> modify(HttpServletRequest request,@RequestBody Employee employee){
        log.info("修改模块");
        //获取session里的数据，更新修改信息
//        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(id);
//        employee.setUpdateTime(LocalDateTime.now());
        //调用service模块,传入数据
        employeeService.updateById(employee);
        return R.success("修改成功");

    }
    @GetMapping("/{id}")
    public R<Employee> employeeById(@PathVariable Long id){
        log.info("根据id查询数据");
        //调用service模块，根据id查询
        Employee employee = employeeService.getById(id);
        //判断employee是否有值
        if(employee!=null){
            return R.success(employee);
        }
        //没有返回值，返回错误信息
        return R.error("没有找到用户");
    }

}
